package com.ruoyi.flowable.listener.move;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.equ.domain.EquMove;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * 移动-1监听
 */
@Slf4j
@Component("moveListener_1")
public class MoveListener_1 implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void notify(DelegateExecution execution) {
        try {
            //资产设备id
            Long assetId = Long.valueOf(execution.getVariable("assetId", String.class));
            //移动类型
            //移动依据:移动类型+原因+移出/移入科室+原/新存放地点
            HashMap<String, Object> moveBasisMap = new HashMap<>();
            String moveType = execution.getVariable("moveType", String.class);
            moveBasisMap.put("移动类型", moveType);
            switch (moveType) {
                case "临时借用":
                    String returnDate = execution.getVariable("returnDate", String.class);
                    String returnLocation = execution.getVariable("returnLocation", String.class);
                    moveBasisMap.put("预计归还日期", returnDate);
                    moveBasisMap.put("预计归还地点", returnLocation);
                    break;
                case "永久调拨":
                    String transferReason = execution.getVariable("transferReason", String.class);
                    moveBasisMap.put("调拨原因", transferReason);
                    break;
                case "维修搬运":
                    String repairDept = execution.getVariable("repairDept", String.class);
                    String repairDuration = execution.getVariable("repairDuration", String.class);
                    moveBasisMap.put("维修部门名称", repairDept);
                    moveBasisMap.put("维修预计时长", repairDuration);
                    break;
                case "科室调整搬迁":
                    String newDept = execution.getVariable("newDept", String.class);
                    String newLocation = execution.getVariable("newLocation", String.class);
                    moveBasisMap.put("新科室", newDept);
                    moveBasisMap.put("新存放地点", newLocation);
                    break;
            }
            moveBasisMap.put("移出科室", execution.getVariable("fromDept", String.class));
            moveBasisMap.put("移出地点", execution.getVariable("fromLocation", String.class));
            moveBasisMap.put("移出联系人", execution.getVariable("fromContact", String.class));
            moveBasisMap.put("移出联系电话", execution.getVariable("fromPhone", String.class));
            String planMoveTime = execution.getVariable("planMoveTime", String.class);
            moveBasisMap.put("计划移动时间", planMoveTime);
            String estimateDuration = execution.getVariable("estimateDuration", String.class);
            moveBasisMap.put("预计移动时长", estimateDuration);
            moveBasisMap.put("移动原因", execution.getVariable("moveReason", String.class));
            moveBasisMap.put("补充说明移动原因", execution.getVariable("moveReasonDesc", String.class));
            if (!moveType.equals("维修搬运")) {
                moveBasisMap.put("移入科室", execution.getVariable("toDept", String.class));
                moveBasisMap.put("移入地点", execution.getVariable("toLocation", String.class));
                moveBasisMap.put("移入联系人", execution.getVariable("toContact", String.class));
                moveBasisMap.put("移入联系电话", execution.getVariable("toPhone", String.class));
            }
            String moveBasis = objectMapper.writeValueAsString(moveBasisMap);

            //执行信息:计划移动时间、时长
            HashMap<String, Object> executeInfoMap = new HashMap<>();
            List<String> specialNeeds = (List<String>) execution.getVariable("specialNeeds");
            HashMap<String, Object> specialNeedsMap = new HashMap<>();
            specialNeedsMap.put("特殊需求", specialNeeds);
            if (specialNeeds.contains("需专业搬运团队")) {
                specialNeedsMap.put("搬运要求", execution.getVariable("carriageReq", String.class));
            }
            if (specialNeeds.contains("需提前断电")) {
                specialNeedsMap.put("断电时间", execution.getVariable("powerOffTime", String.class));
            }
            if (specialNeeds.contains("需校准调试")) {
                specialNeedsMap.put("校准时间", execution.getVariable("calibrateTime", String.class));
            }
            if (specialNeeds.contains("需备用设备")) {
                specialNeedsMap.put("备用设备需求", execution.getVariable("spareDevice", String.class));
            }

            executeInfoMap.put("计划移动时间", planMoveTime);
            executeInfoMap.put("预计移动时长", estimateDuration);
            executeInfoMap.put("特殊需求", specialNeedsMap);
            String executeInfo = objectMapper.writeValueAsString(executeInfoMap);

            EquMove equMove = new EquMove();
            equMove.setAssetId(assetId);
            equMove.setMoveType(moveType);
            equMove.setMoveBasis(moveBasis);
            equMove.setExecuteInfo(executeInfo);
            execution.setVariable("equMove", equMove);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
